/**
 * @Title: feeView_ctrl.js
 * @Description: TODO
 * Copyright: Copyright (c) 2014 
 * Company:中国平安
 *
 * @Project HIMS-FWA
 * @author QIANDUO875
 * @date 2014年9月25日 下午2:06:27
 * @version V1.0
 */
;
(function(){
	'use strict';
	
	var app = angular.module('fwa.mainApp');
	app.controller('CostCoverCtrl',['$scope','$http','BaseURL','FwaTabsetService','$stateParams','Modal','CommunityService',
	                                function($scope,$http,URL,FwaTabsetService,$stateParams,Modal,CommunityService){

		var dateDates = [];
		/**
		 * 获取绘图需要展示的时间
		 */
		$scope.feebar = {};
		function getDate (end) {
			var date = end.split("-");
			
			var dates = [];
			// 当前月份向前推一年
			var year = parseInt(date[0]) - 1;
			var month = parseInt(date[1]) + 1;
			if (month > 12) {
				year = year + 1;
				month = 1;
			}
			var isFlag = true;
			for (var i = 1; i <= 12; i++) {
				if (isFlag && month > 12) {
					year = year + 1;
					month = 1;
					isFlag = false;
				}
				var yearStr = (year+"").substring(2);
				if (month < 10) {
					dates.push(yearStr + "-0" + month);
					dateDates.push(year + "-0" + month);
				} else {
					dates.push(yearStr + "-" + month);
					dateDates.push(year + "-" + month);
				}
				month++;
			}
			$scope.feebar = {
					type:'bar',
					trigger:'axis',
					text:'',
					subtext:'',
					grid:{
						x:40,
						y:25,
						x2:15,
						y2:30,
						borderColor:'#eee'
					},
					legendAlign:'center',
					magicType:false,
					legendData:['药费占比','手术费占比','检查化验费占比','其他'],
					axisData:dates,
					color: ['#dd4814', '#3ca5d0','#FF855A','#006397'],
	            	axisFormatter:'{value}%',
					series:[]
				}; 
		}
		//获取日期
		var endDate = $stateParams.date;
		$scope.communityService = CommunityService;
		CommunityService.initCodeListDatas();
		if (!endDate 
				|| endDate == ''
				|| endDate == null) {
			CommunityService.initDate("yyyy-MM").success(function(res) {
				endDate = CommunityService.lastMonth(res.formatDate);
				getDate (endDate);
			});
		}
		else {
			getDate (endDate);
		}
		$scope.orgLevelDatas = [];
		$scope.$watch('communityService.orgLevelDatas',function(v){
			  if(v){
				  angular.forEach(v,function(data,index){
						if (data.code != '6') {
							 $scope.orgLevelDatas.push(data);
						}
					});
			  }
		  });
		
		 
		var areaOptions = '';
		var debug = false;
		$scope.costFrom = {};
		$scope.costFrom.term = 2;
		$scope.costFrom.hospitalType = "";
		$scope.costFrom.hospitalLev = "";
		$scope.visitType = 0;
		$scope.regionName  = '厦门市';
		//监听按等级或者类型查询数据
	    $scope.$watch("costFrom.term",function(newVal,oldVal){
	        if(newVal == 2 ){
	            $scope.selectLev = true;
	            $scope.selectType = false;
	            $scope.costFrom.hospitalLev = "-1";
	            $scope.costFrom.hospitalType = null;
	        }else if(newVal == 3){
	            $scope.selectLev = false;
	            $scope.selectType = true;
	            $scope.costFrom.hospitalType = "-1";
	            $scope.costFrom.hospitalLev = null;
	        }
	       $scope.getData();
	    });
	    
	    //判断选中的就诊类型
	    $scope.typeClick = function(visitType){
	    	 $scope.visitType = visitType;
	    	 $scope.getData();
	    }
	    var url = "";
	    $scope.getData = function () {	    	
		    if (debug) {
		    	url = URL + 'mock/achievements/settlement/getCostData.json';
		    }
		    else {
		    	url = URL + '/cost/getCostCover.do';
		    }
	    	$http({
				method: 'POST',
				cache:false,
			    url: url,
			    params:{
			    	hospitalType: $scope.costFrom.hospitalType,
			    	hospitalLev: $scope.costFrom.hospitalLev,
			    	visitType: $scope.visitType,
			    	date: endDate
			    }
			}).success(function(response, status, headers, config){
				if(response.data){
					draw(response.data);
					$scope.costList = response.data.costList;
				}else{
					var data = {date:'',medicalPer:'',operationPer:'',checkPer:'',otherPer:''};
					draw(data);
					getCostList();
				}
			});
	    }
	    /**
	     * 如果数据为空时调用
	     */
	    function getCostList() {
	    	var costDatas = [];
	    	angular.forEach(dateDates,function(data,index){
	    		var cost = {
	    				date:data,
	    				medicalPer:"-",
	    				operationPer:"-",
	    				checkPer:"-",
	    				otherPer:"-"
	    		};
	    		costDatas.push(cost);
	    	});
	    	$scope.costList = costDatas;
	    }
	    //绘制柱状图
	   function draw(data) {
		   $scope.feebar.series =[{
	            name:'药费占比',
	            type:'bar',
	            data:data.medicalPer
	        },
	        {
	            name:'手术费占比',
	            type:'bar',
	            data:data.operationPer
	        },
	        {
	            name:'检查化验费占比',
	            type:'bar',
	            data:data.checkPer
	        },
	        {
	            name:'其他',
	            type:'bar',
	            data:data.otherPer
	        }];
	    }
	   
	   //查看明细
	   $scope.queryAnalyse = function() {
			FwaTabsetService.selectTab('settleAnalyse.regionView',{
				indicator: 'KPI0005',
				startDate: '',
				endDate: endDate
			},false);
		}

		
	}]);

	
})();